from django_filters.rest_framework.filterset import FilterSet
from django_filters import filters, DateFilter, DateTimeFilter
from api.models import WmsItemSku, WmsInventory



class ItemSkuFilter(FilterSet):
    # 过滤 item.itemName（假设 item 是 ForeignKey 关联到 WmsItem）
    itemName = filters.CharFilter(
        field_name="item__item_name",  # 使用双下划线访问关联字段
        lookup_expr="icontains",      # 支持 icontains/exact/iexact 等
        label="Item Name"
    )

    # 过滤 item.itemCode
    itemCode = filters.CharFilter(
        field_name="item__item_code",
        lookup_expr="exact",
        label="Item Code"
    )

    # 过滤 itemSku.skuName（假设 skuName 是 WmsItemSku 的字段）
    skuName = filters.CharFilter(
        field_name="sku_name",         # 如果是当前模型的字段，直接写字段名
        lookup_expr="icontains",
        label="SKU Name"
    )

    # 过滤 itemSku.barcode
    barcode = filters.CharFilter(
        field_name="barcode",         # 假设 barcode 是 WmsItemSku 的字段
        lookup_expr="exact",          # 条码一般精确匹配
        label="Barcode"
    )

    class Meta:
        model = WmsItemSku
        fields = []  # 不自动生成过滤器，完全手动控制

class BoardListFilter(FilterSet):
    product_id = filters.CharFilter(method='filter_by_product_ids')

    class Meta:
        model = WmsInventory
        fields = [] 
        
    def filter_by_product_ids(self, queryset, name, value):
        ids = [int(id) for id in value.split(',')]
        return queryset.filter(product_id__in=ids)